package com.qf.admin.controller;


import com.qf.common.http.Result;
import com.qf.dto.Goods;
import com.qf.dto.GoodsType;
import com.qf.service.impl.GoodsService;
import com.qf.service.impl.GoodsTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    @Resource
    private GoodsTypeService goodsTypeService;

    @RequestMapping("query")
    public String toDel(Model model){
        List<Goods> goods = goodsService.list();
        model.addAttribute("goodsList",goods);
        List<GoodsType> goodsTypes = goodsTypeService.list();
        model.addAttribute("goodsTypes",goodsTypes);
        return "goods/goods";
    }

    @RequestMapping("del")
    @ResponseBody
    public Result<?> toDel(@RequestParam(value = "id", required = true)Long id){
        goodsService.removeById(id);
        return Result.success();
    }


    //上架和下架可以通过State的状态码来更改   在service层中可以定义上架和下架的两个方法
    //在实体类中定义枚举用来记录是否上架和下架  枚举中有个code状态码
    //当调用上架和下架的方法的时候就相应的走了service层中设置了state枚举中的相应的信息 , 这样就可以实现上架和下架的功能
    @RequestMapping("up")
    @ResponseBody
    public Result<?> toUp(@RequestParam(value = "id", required = true)  Long goodsId){
        goodsService.up(goodsId);

        return Result.success();
    }

    @RequestMapping("down")
    @ResponseBody
    public Result<?> toDown(@RequestParam(value = "id", required = true)  Long goodsId){
        goodsService.down(goodsId);

        return Result.success();
    }

}
